package com.ideaaedi.commonspring.aop;

import com.ideaaedi.commonspring.filter.TimeWatcherFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.AnnotationAttributes;

import java.util.Objects;

/**
 * (non-javadoc)
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img
 * src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 1.0.0
 */
@Slf4j
public class TimeWatcherFilterConfig {
    
    public static final String BEAN_NAME = "timeWatcherFilterConfig";
 
    private final AnnotationAttributes annotationAttributes;
    
    public TimeWatcherFilterConfig(AnnotationAttributes annotationAttributes) {
        this.annotationAttributes = Objects.requireNonNull(annotationAttributes, "annotationAttributes should not be null.");
    }
    
    @Bean(name = TimeWatcherFilter.BEAN_NAME)
    public TimeWatcherFilter timeWatcherFilter(ApplicationContext applicationContext) {
        // 参数信息
        int filterPriority = (int)annotationAttributes.get("filterPriority");
        boolean prettyResult = (boolean)annotationAttributes.get("prettyResult");
        log.info("registry bean '{}' with ConstructorArg filterPriority -> {}, prettyResult -> {}",
                TimeWatcherFilter.BEAN_NAME, filterPriority, prettyResult);
        return new TimeWatcherFilter(filterPriority, prettyResult, applicationContext);
    }
}
